﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class JLTVKAV95 : DocumentBase
    {
        #region=============Properties=======================
        string Mandant = "00";
        bool IsLoaded = false;
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;
        public OracleConnection OracleDB;
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        JLTVKAV95Data m_Data;
        INTEXDBC INTEXDBC = null;
        public DataSet setResult;
        BackgroundWorker bgWoker;
        JLTVKAV95Report m_rpt;
        string UserID = "";
        #region==========CollectionViewSource=================
        SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetData");
            }
        }
       
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if (vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        }
        string KRS
        {
            get
            {
                return Helpers.Helper.Left(txtVKPAUFKRS.Text.ToString().PadRight(2), 2);

            }
            set
            {
                txtVKPAUFKRS.Text = value;
            }
        }
        string LFD
        {
            get
            {
                return Helpers.Helper.Right("000000" + txtVKPAUFLFD.Text.ToString(), 6);

            }
            set
            {
                txtVKPAUFLFD.Text = value;
            }
        } 
        BindingListCollectionView cvXDECK
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dgVKPOS.ItemsSource);
            }
        }  
        #endregion
 
        public JLTVKAV95()
        {
            InitializeComponent();
            this.Name = "订单跟踪";            
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        { 
            using (new LongOperation(this, "数据更新中"))
            {
                bgWoker.RunWorkerAsync();
            }
        }
        public override void OnWorkflowIDChange(string wfid)
        {
            base.OnWorkflowIDChange(wfid);
            try
            {
                dwWFEvent = null;
                DataSet SResult = m_Data.INTEXDBC.QueryICC_WFEVENTS(wfid, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwWFEvent = row;
                }
                if (dwWFEvent == null)
                    return;
                string events = dwWFEvent["WFEVENTS"].ToString().Trim();
                switch (events)
                {
                    case "A901003"://主面料已经审核，辅料可以采购
                        KRS = dwWFEvent["KRS"].ToString();
                        LFD = dwWFEvent["LFD"].ToString();
                        btnQuery_Click(null, null);
                        if (vwVKPOS.View != null)
                            vwVKPOS.View.MoveCurrentToPosition(0);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    dicParamater = ApplicationBase.dicParamater;                
                }
                if (IsLoaded)
                    return;
                IsLoaded = true; 
                btnReport.IsEnabled = true;
                btnPrintPreview.IsEnabled = true;                 
                vwVKPOS.View.CurrentChanged += vwVKPOS_CurrentChanged;
                bgWoker = new BackgroundWorker();
                bgWoker.DoWork += bgWoker_DoWork;
                bgWoker.RunWorkerCompleted += bgWoker_RunWorkerCompleted;
                btnRefresh_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }
        void bgWoker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("更新完成", StatusEventType.StopProgress);
        }

        void bgWoker_DoWork(object sender, DoWorkEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("开始更新", StatusEventType.StartProgress);
            m_Data.Refresh(null);
        } 
        void vwVKPOS_CurrentChanged(object sender, EventArgs e)
        {
           
            try
            {
             
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        } 
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                    this.dicParamater = dicParamater;
                }
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];

                m_Data = new JLTVKAV95Data(OracleDB, proxy);          
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
      
        public void GenerateReport(string PrintType, bool isapproved = false)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {

                    JLTVKAV95Data m_DX = new JLTVKAV95Data(dicParamater);
                    JLTVKAV95Report m_rpt = new JLTVKAV95Report(dicParamater);
                    m_rpt.dsSource = SetData;
                    m_rpt.PrintType = PrintType;
                    m_rpt.exAgent = m_DX.exAgent;
                    m_rpt.OracleDB = this.OracleDB;
                    m_rpt.proxy = this.proxy;
                    m_rpt.IsApproved = isapproved;
                    m_rpt.WriteExcelReport(PrintType);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
           
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
            try
            {
         
                DataSet setResult = new DataSet();
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataRow dwData = SetData.ICC_VKAV95.NewRow();
                    dwData["VKPMANDANT"] = "00";
                    dwData["VKPAUFKRS"] = txtVKPAUFKRS.Text;
                    dwData["VKPAUFLFD"] = Helper.Right("000000"+txtVKPAUFLFD.Text.ToString(),6);
                    setResult = m_Data.GetData(dwData);
                    SetData.Clear();
                    SetData.Merge(setResult);
                    if (vwVKPOS.View != null)
                    {
                        vwVKPOS.View.MoveCurrentToFirst();
                    }
                }           
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            EventDispatcher.Instance.RaiseStatus("查询结束", StatusEventType.StopProgress);
        }

        
        
        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            cvXDECK.CustomFilter = null;
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            cvXDECK.CustomFilter = null;
        }
 

        private void txtVKPAUFLFD_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.btnQuery_Click(null, null);
            } 
        }
    }
}
